//
//  AllHotCountryViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/16.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "AllHotCountryViewController.h"
#import "AllHotCouCell.h"
#import "CountryModel.h"
#import "CityViewController.h"

@interface AllHotCountryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, strong) UICollectionView *hotCouCollect;
@property (nonatomic, strong) NSMutableArray *cityArr;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation AllHotCountryViewController

// 视图的出现将tabbar隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)creatData {
    self.page++;
    NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/place/city/get_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&countryid=%@&lat=38.88265581038946&lon=121.5397240097761&oauth_token=3ccf6be2c77dee4d33e6d6a5eb878fc7&page=%ld&track_app_channel=App%%2520Store&track_app_version=6.6&track_device_info=iPhone7%%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%%25208.1.2&track_user_id=6654242&v=1%%20",self.allCity,self.page];
    [ZSMAFNetWork geturl:str body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        NSArray *arr = result[@"data"];
        for (NSDictionary *dic in arr) {
            CountryModel *model = [[CountryModel alloc]initWithDic:dic];
            [self.cityArr addObject:model];
        }
        [self.hotCouCollect reloadData];
        self.hud.hidden = YES;
        [self.hotCouCollect.footer endRefreshing];
    } failure:^(NSError *error) {
        NSLog(@"错误是:%@",error);
        
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
    
    self.cityArr = [NSMutableArray array];
    [self creatData];
    [self creatView];
    NSString *title = [NSString stringWithFormat:@"%@城市",self.placeName];
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.text = title;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;

    // Do any additional setup after loading the view.
    // 左上角返回图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];
}

// 返回
- (void)leftAction:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)creatView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(WIDTH * 177.5, HEIGHT * 172.5);
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
    
    self.hotCouCollect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 667) collectionViewLayout:flowLayout];
    [self.view addSubview:self.hotCouCollect];
    [self.hotCouCollect registerClass:[AllHotCouCell class] forCellWithReuseIdentifier:@"allHotCell"];
    self.hotCouCollect.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.910];
    self.hotCouCollect.delegate = self;
    self.hotCouCollect.dataSource = self;
    self.hud = [MBProgressHUD showHUDAddedTo:self.hotCouCollect animated:YES];
    [self.hud hide:YES afterDelay:2];
    self.hotCouCollect.emptyDataSetSource = self;
    self.hotCouCollect.emptyDataSetDelegate = self;
    
    [self.hotCouCollect mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);        
    }];

    [self createMJRefresh];
}

// 空白页占位图
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, self.view.bounds.size.height/2, WIDTH*375, 30)];
    label.text = @"! 网速不给力";
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = [UIColor redColor];
    return label;
}

- (void)createMJRefresh {
    // 上拉加载
    self.hotCouCollect.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(creatData)];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.cityArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AllHotCouCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"allHotCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    CountryModel *model = self.cityArr[indexPath.row];
    [cell.bomView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"F"]];
    cell.cnLabel.text = model.catename;
    cell.enLabel.text = model.catename_en;
    cell.perNumLabel.text = model.beenstr;
    if (![model.representative isEqualToString:@""]) {
        cell.teLabel.text = [NSString stringWithFormat:@"代表景点 :%@",model.representative];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CityViewController *cVC = [[CityViewController alloc]init];
    CountryModel *model = self.cityArr[indexPath.row];
    cVC.cityId = model.uId;
    [self.navigationController pushViewController:cVC animated:YES];
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.hotCouCollect.backgroundColor = [UIColor blackColor];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    else {
        self.hotCouCollect.backgroundColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor whiteColor];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
